﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;
using Seagull.Configuration;
using Seagull.Controls;
using Seagull.Events.Data;
using Seagull.Model;
using Seagull.Shell.Properties;
using Seagull.Shell.Utils;

namespace Seagull.Shell.Views
{
    public partial class DeployView : BaseView
    {
        private readonly Image _map;
        private readonly ToolTip _channelToolTip;
        private readonly ShapeContainer _shapeContainer;
        private readonly MarkerBlinkingManager _markerBlinkingManager;

        private readonly Dictionary<byte, OvalShape> _channelMarkers = new Dictionary<byte, OvalShape>();

        public DeployView()
        {
            InitializeComponent();

            HitTest = true;

            _map = Resources.gis_3D;
            _shapeContainer = new ShapeContainer { Parent = this };
            _channelToolTip = new ToolTip
            {
                // Set up the delays for the ToolTip.
                AutoPopDelay = 5000,
                InitialDelay = 1000,
                ReshowDelay = 500,
                // Force the ToolTip text to be displayed whether or not the form is active.
                ShowAlways = true
            };
            _markerBlinkingManager = new MarkerBlinkingManager(_blinkTimer);
        }

        public int ChannelMarkerRadius
        {
            get
            {
                if(Width < 640 & Height < 480)
                {
                    return 7;
                }
                else
                {
                    return MapHelper.MarkerRadius;
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Config.Instance.ChannelConfig.ForAllChannels(channel =>
            {
                var marker = DrawChannelMarker(Config.Instance.ChannelConfig[channel]);
                _channelMarkers.Add(channel, marker);
            });

            NewFrameEvent.Instance.Register(OnNewFrame);
        }

        /// <summary>
        /// 决定该控件是否响应鼠标事件
        /// </summary>
        public bool HitTest { get; set; }

        private void OnNewFrame(Frame frame)
        {
            if(Config.Instance.ChannelConfig.HasChannel(frame.Channel))
            {
                _markerBlinkingManager.Blink(
                    _channelMarkers[frame.Channel],
                    FrameDataWarningHelper.GetWarningLevel(frame));
            }
        }

        private OvalShape DrawChannelMarker(ChannelInfo channel)
        {
            var markerRadius = ChannelMarkerRadius;
            var shape = MapHelper.DrawMarker(
                MapHelper.FromPointOnMap(
                    new Point(channel.X, channel.Y),
                    this.Size, _map.Size),
                _shapeContainer, markerRadius);

            var hoverSeen = false;
            shape.MouseHover += (sender, e) =>
            {
                if(!hoverSeen && HitTest)
                {
                    hoverSeen = true;
                    _channelToolTip.Show(channel.Name, this,
                        PointToClient(Cursor.Position) + new Size(0, Cursor.Size.Height));
                }
            };
            shape.MouseLeave += (sender, e) =>
            {
                if(hoverSeen)
                {
                    _channelToolTip.Hide(this);
                }
                hoverSeen = false;
            };
            shape.Click += OnChannelMarkerClick;

            return shape;
        }

        void OnChannelMarkerClick(object sender, EventArgs e)
        {
            if(HitTest)
            {
                _channelToolTip.Hide(this);
                OnViewChangeRequested(ViewKind.Chart);
            }
        }

        private void DeployView_Click(object sender, EventArgs e)
        {
            //            OnViewChangeRequested(ViewKind.Chart);

        }
    }
}
